'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 离开战斗
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var _ = require('lodash');
var Code = require('../../../../consts/code');
var moduleType = require('../../../../consts/moduleType');
var operationType = require('../../../../consts/operationType');
var battleRecordModel = require('../../../../models/battleRecordModel');
var heroConf = require('../../../../config/heroConf');
var pvpConf = require('../../../../config/pvpConf.js');
var battleConf = require('../../../../config/battleConf');
var fightRewardUtil = require('../../../../util/fightRewardUtil');
var constantUtil = require('../../../../util/constantUtil');
var roleService = require('../../../../services/roleService');
var battleService = require('../../../../services/battleService');
var PlayerManager = require('../../../../libs/playerManager');
var dataApi = require('../../../../libs/dataApi');
var genRewardService = require('../../../../services/genRewardService');
var rewardService = require('../../../../services/rewardService');
var activityService = require('../../../../services/activityService.js');
var actType = require('../../../../consts/activityType.js');
var taskService = require('../../../../services/taskService');

module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var pvp_guide_id = msg.pvp_guide_id;
    if (!pvp_guide_id) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var player_manager = new PlayerManager(role_id);
    var reward_list = [];
    var add_honor = 0;
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['role', 'pvp', 'battle', 'bag', 'soldier', 'build', 'task', 'daily_task', 'achieve', 'tech', 'sevenDayActivity', 'accrued'],
                    cb);
            },
            function (cb) {
                var role_model = player_manager.role;
                var battle_model = player_manager.battle;
                var pvp_model = player_manager.pvp;

                if (_.indexOf(battle_model.reward_guide_list, pvp_guide_id) !== -1) {
                    return next(null, {
                        code: Code.BATTLE_GUIDE_REWARD
                    });
                }

                battle_model.reward_guide_list.push(pvp_guide_id);

                var pvp_guide_reward_conf = dataApi.pvp_guide_reward.findById(role_model.getJob() * 100 + pvp_guide_id);
                _.each(pvp_guide_reward_conf, function (guide_reward) {
                    reward_list.push(genRewardService.generateReward(guide_reward));
                });
                rewardService.addRewardListToRole(player_manager, reward_list, moduleType.MDL_PVP, operationType.OPR_PVP_FIGHT);

                roleService.addGoldLimit(player_manager, moduleType.MDL_PVP, operationType.OPR_PVP_FIGHT, 500);
                roleService.addFoodLimit(player_manager, moduleType.MDL_PVP, operationType.OPR_PVP_FIGHT, 500);
                roleService.addDragonGold(player_manager, moduleType.MDL_PVP, operationType.OPR_PVP_FIGHT, 1);

                // 增加军功宝箱星数
                var star = 3;
                pvp_model.addHonorBoxStar(star);

                //增加军功
                var pvp_star_conf = pvpConf.getPvpStarConf(star);
                add_honor = pvp_star_conf.honor;
                roleService.addHonor(player_manager, add_honor);

                battle_model.addCup(1);
                role_model.noble = battle_model.getNoble();
                activityService.push(role_id, actType.RANK, player_manager);

                // 加入任务统计
                taskService.addBattleInNum(player_manager);
                player_manager.save(cb);
            },
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }

            var pvp_guide_conf = _getPvpGuideHeroConf(pvp_guide_id);
            return next(null, {
                code: Code.OK,
                result: {
                    def_name: pvp_guide_conf.name,
                    add_gold: 0,
                    add_food: 0,
                    add_exp: 0,
                    add_cup: 1,
                    star: 3,
                    add_honor: add_honor,
                    progress: 100,
                    add_train_exp_soldier_list: [],
                    reward_list: reward_list
                }
            });
        }
    );
};

var _getPvpGuideHeroConf = function (pvp_guide_id) {
    return dataApi.pvp_guide_hero.findById(pvp_guide_id);
};
